<div class="selenium-notes">
<h1>Parent and Child XPath in Selenium</h1>
<p><strong>Parent and Child XPath</strong> is an important XPath concept used in Selenium to locate web elements based on their hierarchical relationship in the HTML DOM. When an element does not have a unique ID, name, class, or other reliable attribute, understanding the relationship between parent and child elements can help create accurate XPath expressions.</p>
<p>In HTML, elements are arranged in a hierarchical structure. An element can contain one or more child elements, while the element directly containing another element is called its parent. Selenium XPath allows testers to move between these related elements using axes such as <strong>parent</strong>, <strong>child</strong>, and descendant-related expressions.</p>
<p>XPath and locator strategies are part of Selenium WebDriver automation training, alongside web element handling, browser automation, TestNG, dynamic elements, and automation frameworks. <a href="https://www.justacademy.co/course-detail/selenium-training" target="_blank" rel="noopener noreferrer">JustAcademy Selenium Training</a> covers these Selenium automation concepts through practical learning. You can also explore the <a href="https://www.justacademy.co/register-for-course-demo" target="_blank" rel="noopener noreferrer">JustAcademy Course Demo Registration</a> page.</p>
<hr>
<h2>1. What is XPath?</h2>
<p>XPath stands for <strong>XML Path Language</strong>. It is a language used to navigate and identify nodes in XML and HTML documents.</p>
<p>In Selenium, XPath is widely used as a locator strategy for finding web elements on a web page.</p>
<h3>Basic XPath Syntax</h3>
<pre><code>//tagname[@attribute='value']</code></pre>
<p>Example:</p>
<pre><code>//input[@id='username']</code></pre>
<p>This XPath identifies an input element whose ID is <strong>username</strong>.</p>
<hr>
<h2>2. What is the DOM?</h2>
<p>DOM stands for <strong>Document Object Model</strong>. A browser converts an HTML document into a tree-like structure called the DOM.</p>
<p>For example:</p>
<pre><code>html
├── body
│ ├── div
│ │ ├── label
│ │ └── input
│ └── button</code></pre>
<p>The DOM structure allows XPath to navigate between elements according to their relationships.</p>
<hr>
<h2>3. What is a Parent Element?</h2>
<p>A <strong>parent element</strong> is an HTML element that directly contains another element.</p>
<h3>Example</h3>
<pre><code><div class="login-form">
<input type="text" id="username">
</div></code></pre>
<p>Here:</p>
<ul>
<li>The <code>div</code> is the parent of the input.</li>
<li>The <code>input</code> is the child of the div.</li>
</ul>
<p>The relationship can be represented as:</p>
<pre><code>div
└── input</code></pre>
<hr>
<h2>4. What is a Child Element?</h2>
<p>A <strong>child element</strong> is an element that is directly contained inside another element.</p>
<h3>Example</h3>
<pre><code><div>
<button>Login</button>
</div></code></pre>
<p>Here:</p>
<ul>
<li>The div is the parent.</li>
<li>The button is the child.</li>
</ul>
<hr>
<h2>5. Parent-Child Relationship in HTML</h2>
<p>Consider the following HTML:</p>
<pre><code><div class="login">
<label>Username</label>
<input type="text">
<button>Login</button>
</div></code></pre>
<p>The DOM structure is:</p>
<pre><code>div.login
├── label
├── input
└── button</code></pre>
<p>The div is the parent of the label, input, and button.</p>
<hr>
<h2>6. Child XPath</h2>
<p>The <strong>child</strong> axis is used to select direct child elements of the current element.</p>
<h3>Syntax</h3>
<pre><code>//parent/child::childElement</code></pre>
<p>Example:</p>
<pre><code>//div/child::input</code></pre>
<p>This selects input elements that are direct children of the div.</p>
<hr>
<h2>7. Short Syntax for Child XPath</h2>
<p>The <strong>child::</strong> axis is often omitted because a simple slash represents direct child navigation.</p>
<p>For example:</p>
<pre><code>//div/child::input</code></pre>
<p>can be written as:</p>
<pre><code>//div/input</code></pre>
<p>Both expressions represent direct child navigation in this context.</p>
<hr>
<h2>8. Child XPath with Attributes</h2>
<p>Attributes can be used to make the child XPath more specific.</p>
<pre><code>//div/child::input[@type='text']</code></pre>
<p>Short form:</p>
<pre><code>//div/input[@type='text']</code></pre>
<p>This identifies a text input that is a direct child of a div.</p>
<hr>
<h2>9. Child XPath with Multiple Child Elements</h2>
<p>Suppose the HTML contains:</p>
<pre><code><div class="form">
<input type="text" id="username">
<input type="password" id="password">
<button>Login</button>
</div></code></pre>
<p>Possible XPath expressions include:</p>
<pre><code>//div[@class='form']/input[@id='username']</code></pre>
<pre><code>//div[@class='form']/input[@id='password']</code></pre>
<pre><code>//div[@class='form']/button[text()='Login']</code></pre>
<hr>
<h2>10. Parent XPath</h2>
<p>The <strong>parent</strong> axis is used to move from the current element to its direct parent.</p>
<h3>Syntax</h3>
<pre><code>//childElement/parent::parentElement</code></pre>
<p>Example:</p>
<pre><code>//input[@id='username']/parent::div</code></pre>
<p>This starts from the username input and moves to its parent div.</p>
<hr>
<h2>11. Short Syntax for Parent XPath</h2>
<p>The parent relationship can also be represented using <strong>..</strong>.</p>
<pre><code>//input[@id='username']/..</code></pre>
<p>This moves from the username input to its direct parent.</p>
<p>The explicit parent axis can be written as:</p>
<pre><code>//input[@id='username']/parent::div</code></pre>
<p>The explicit axis is often easier to understand because it clearly describes the relationship.</p>
<hr>
<h2>12. Parent XPath with Attributes</h2>
<p>The parent element can be filtered using its attributes.</p>
<pre><code>//input[@id='username']/parent::div[@class='login-form']</code></pre>
<p>This identifies the parent div only when it has the specified class.</p>
<hr>
<h2>13. Parent and Child XPath Together</h2>
<p>Parent and child relationships can be combined to navigate through the DOM.</p>
<pre><code>//div[@class='login-form']/child::input</code></pre>
<p>This starts from the login form div and selects its direct input children.</p>
<p>Reverse navigation can be performed using:</p>
<pre><code>//input[@id='username']/parent::div</code></pre>
<p>This starts from the input and moves upward to the parent div.</p>
<hr>
<h2>14. Parent XPath Example</h2>
<h3>HTML</h3>
<pre><code><div class="user-form">
<input id="username">
</div></code></pre>
<h3>XPath</h3>
<pre><code>//input[@id='username']/parent::div</code></pre>
<p>Result: the <code>div</code> containing the username input.</p>
<hr>
<h2>15. Child XPath Example</h2>
<h3>HTML</h3>
<pre><code><div class="user-form">
<input id="username">
</div></code></pre>
<h3>XPath</h3>
<pre><code>//div[@class='user-form']/child::input</code></pre>
<p>Result: the username input element.</p>
<hr>
<h2>16. Parent vs Child XPath</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Feature</th><th>Parent XPath</th><th>Child XPath</th></tr>
<tr><td>Direction</td><td>Moves upward</td><td>Moves downward</td></tr>
<tr><td>Relationship</td><td>Child to parent</td><td>Parent to child</td></tr>
<tr><td>Axis</td><td>parent::</td><td>child::</td></tr>
<tr><td>Short form</td><td>..</td><td>/child element</td></tr>
<tr><td>Example</td><td><code>//input/parent::div</code></td><td><code>//div/child::input</code></td></tr>
</table>
<hr>
<h2>17. Parent vs Ancestor</h2>
<p>A parent is the immediate element containing the current element. An ancestor can be the parent, grandparent, or any higher-level element.</p>
<h3>HTML</h3>
<pre><code><div class="page">
<form>
<div class="field">
<input id="email">
</div>
</form>
</div></code></pre>
<p>The direct parent of the input is:</p>
<pre><code>//input[@id='email']/parent::div</code></pre>
<p>An ancestor form can be located using:</p>
<pre><code>//input[@id='email']/ancestor::form</code></pre>
<hr>
<h2>18. Parent vs Ancestor Difference</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Parent</th><th>Ancestor</th></tr>
<tr><td>Selects the immediate parent</td><td>Can select parent, grandparent, or higher-level elements</td></tr>
<tr><td>Moves one level upward</td><td>Can move multiple levels upward</td></tr>
<tr><td><code>parent::</code></td><td><code>ancestor::</code></td></tr>
</table>
<hr>
<h2>19. Child vs Descendant</h2>
<p>A child is a direct child of the current element. A descendant can be located at any depth below the current element.</p>
<h3>HTML</h3>
<pre><code><div>
<section>
<button>Login</button>
</section>
</div></code></pre>
<p>The button is not a direct child of the div because the section is between them.</p>
<p>Direct child:</p>
<pre><code>//div/child::section</code></pre>
<p>Descendant:</p>
<pre><code>//div/descendant::button</code></pre>
<hr>
<h2>20. Child vs Descendant Difference</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Child</th><th>Descendant</th></tr>
<tr><td>Only direct children</td><td>Elements at any level below the current node</td></tr>
<tr><td><code>child::</code></td><td><code>descendant::</code></td></tr>
<tr><td>Example: <code>//div/child::input</code></td><td>Example: <code>//div/descendant::input</code></td></tr>
</table>
<hr>
<h2>21. Parent XPath with text()</h2>
<p>Text-based XPath can be combined with the parent axis.</p>
<h3>HTML</h3>
<pre><code><div class="field">
<label>Username</label>
<input type="text">
</div></code></pre>
<p>Locate the parent div using the label:</p>
<pre><code>//label[text()='Username']/parent::div</code></pre>
<p>This identifies the div that directly contains the Username label.</p>
<hr>
<h2>22. Child XPath with text()</h2>
<p>Text conditions can also be used when selecting child elements.</p>
<pre><code>//div[@class='menu']/child::button[text()='Login']</code></pre>
<p>This searches for a Login button that is a direct child of the menu div.</p>
<hr>
<h2>23. Parent XPath with contains()</h2>
<p>The contains() function can be combined with parent navigation.</p>
<pre><code>//span[contains(text(),'Username')]/parent::div</code></pre>
<p>This finds the parent div of a span containing Username.</p>
<hr>
<h2>24. Child XPath with contains()</h2>
<pre><code>//div[@class='menu']/child::button[contains(text(),'Login')]</code></pre>
<p>This locates a direct child button containing Login in its text.</p>
<hr>
<h2>25. Parent XPath in Login Forms</h2>
<p>Login forms often contain labels, input fields, and buttons grouped inside parent containers.</p>
<h3>Example HTML</h3>
<pre><code><div class="login-field">
<label>Username</label>
<input type="text" id="username">
</div></code></pre>
<p>Locate the parent:</p>
<pre><code>//input[@id='username']/parent::div</code></pre>
<p>Locate the parent using the label:</p>
<pre><code>//label[text()='Username']/parent::div</code></pre>
<hr>
<h2>26. Child XPath in Login Forms</h2>
<p>Suppose the login form has several direct child controls.</p>
<pre><code><div class="login-form">
<input id="username">
<input id="password">
<button>Login</button>
</div></code></pre>
<p>Username:</p>
<pre><code>//div[@class='login-form']/child::input[@id='username']</code></pre>
<p>Password:</p>
<pre><code>//div[@class='login-form']/child::input[@id='password']</code></pre>
<p>Login button:</p>
<pre><code>//div[@class='login-form']/child::button[text()='Login']</code></pre>
<hr>
<h2>27. Parent XPath in E-Commerce Applications</h2>
<p>Product cards commonly use parent containers to group product names, prices, images, and action buttons.</p>
<h3>Example HTML</h3>
<pre><code><div class="product-card">
<h3>Laptop</h3>
<span>₹50000</span>
<button>Add to Cart</button>
</div></code></pre>
<p>Locate the parent product card using the product name:</p>
<pre><code>//h3[text()='Laptop']/parent::div</code></pre>
<hr>
<h2>28. Child XPath in E-Commerce Applications</h2>
<p>After identifying the product card, child XPath can locate specific controls inside it.</p>
<pre><code>//div[@class='product-card']/child::button[text()='Add to Cart']</code></pre>
<p>Price:</p>
<pre><code>//div[@class='product-card']/child::span</code></pre>
<p>Product name:</p>
<pre><code>//div[@class='product-card']/child::h3</code></pre>
<hr>
<h2>29. Parent-Child XPath with Product Name</h2>
<p>A more specific expression can identify a product card and then locate its button.</p>
<pre><code>//div[h3[text()='Laptop']]/child::button[text()='Add to Cart']</code></pre>
<p>This searches for a div containing an h3 with the text Laptop and then selects its direct Add to Cart button.</p>
<hr>
<h2>30. Parent XPath in Tables</h2>
<p>Tables contain rows and cells with clear parent-child relationships.</p>
<h3>HTML</h3>
<pre><code><table>
<tr>
<td>John</td>
<td>Active</td>
</tr>
</table></code></pre>
<p>Locate the row containing John:</p>
<pre><code>//td[text()='John']/parent::tr</code></pre>
<p>This moves from the John cell to its parent row.</p>
<hr>
<h2>31. Child XPath in Tables</h2>
<p>Once the row is identified, child navigation can locate its cells.</p>
<pre><code>//tr[td[text()='John']]/child::td</code></pre>
<p>This selects the direct td children of the row containing John.</p>
<p>To locate the Active cell:</p>
<pre><code>//tr[td[text()='John']]/child::td[text()='Active']</code></pre>
<hr>
<h2>32. Parent XPath for Table Rows</h2>
<p>Parent navigation is useful when the target information is inside a table cell but the required interaction is associated with the entire row.</p>
<pre><code>//td[text()='John']/parent::tr</code></pre>
<p>The result is the table row containing John.</p>
<hr>
<h2>33. Child XPath for Table Actions</h2>
<p>Consider a table row containing an Edit button:</p>
<pre><code><tr>
<td>John</td>
<td>Active</td>
<td><button>Edit</button></td>
</tr></code></pre>
<p>Locate the Edit button from the row:</p>
<pre><code>//tr[td[text()='John']]/child::td/child::button[text()='Edit']</code></pre>
<hr>
<h2>34. Parent XPath with Multiple Levels</h2>
<p>Sometimes the immediate parent is not the required container. Multiple parent navigation steps can be used.</p>
<pre><code>//input[@id='email']/parent::div/parent::form</code></pre>
<p>This moves from:</p>
<pre><code>input
→ parent div
→ parent form</code></pre>
<p>However, when the exact ancestor is known, the ancestor axis can often make the expression simpler.</p>
<hr>
<h2>35. Child XPath with Multiple Levels</h2>
<p>Child navigation can also be chained.</p>
<pre><code>//form/child::div/child::input</code></pre>
<p>This navigates:</p>
<pre><code>form
→ direct child div
→ direct child input</code></pre>
<hr>
<h2>36. Parent XPath Using ..</h2>
<p>The double-dot notation <strong>..</strong> represents the parent node.</p>
<pre><code>//input[@id='username']/..</code></pre>
<p>This selects the immediate parent of the username input.</p>
<p>Multiple levels can be navigated:</p>
<pre><code>//input[@id='username']/../..</code></pre>
<p>This moves up two levels.</p>
<hr>
<h2>37. Parent Axis vs ..</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Method</th><th>Example</th><th>Description</th></tr>
<tr><td>Parent axis</td><td><code>//input/parent::div</code></td><td>Explicitly identifies the parent relationship</td></tr>
<tr><td>Parent shorthand</td><td><code>//input/..</code></td><td>Moves to the immediate parent</td></tr>
</table>
<p>The explicit parent axis can be easier to read and maintain in complex XPath expressions.</p>
<hr>
<h2>38. Child Axis vs /</h2>
<p>The slash can represent direct child navigation.</p>
<pre><code>//div/input</code></pre>
<p>Equivalent explicit axis:</p>
<pre><code>//div/child::input</code></pre>
<p>Both expressions target an input that is a direct child of the div in this structure.</p>
<hr>
<h2>39. Parent and Child XPath with Attributes</h2>
<p>Attributes can be combined with parent-child relationships to create more precise locators.</p>
<pre><code>//input[@name='email']/parent::div[@class='form-group']</code></pre>
<p>Child navigation:</p>
<pre><code>//div[@class='form-group']/child::input[@name='email']</code></pre>
<hr>
<h2>40. Parent and Child XPath with Text</h2>
<p>Text and attributes can be combined with parent-child relationships.</p>
<pre><code>//label[text()='Email']/parent::div/child::input</code></pre>
<p>This can locate an input that is a direct child of the parent div containing the Email label.</p>
<hr>
<h2>41. Parent and Child XPath with normalize-space()</h2>
<p>When label text contains extra whitespace, normalize-space() can be used.</p>
<pre><code>//label[normalize-space()='Username']/parent::div/child::input</code></pre>
<p>This is useful when formatting or whitespace in the HTML affects exact text matching.</p>
<hr>
<h2>42. Parent and Child XPath with contains()</h2>
<p>When only part of the text is stable, contains() can be used.</p>
<pre><code>//label[contains(text(),'Email')]/parent::div/child::input</code></pre>
<p>This searches for an Email-related label, moves to its parent div, and selects a direct child input.</p>
<hr>
<h2>43. Parent-Child XPath with Following</h2>
<p>Parent-child navigation can be combined with following-axis navigation.</p>
<pre><code>//label[text()='Username']/parent::div/following::div[1]</code></pre>
<p>This identifies the parent div of the Username label and then searches for the next matching div in document order.</p>
<hr>
<h2>44. Parent-Child XPath with Preceding</h2>
<p>Similarly, preceding can be used after moving to a parent.</p>
<pre><code>//input[@id='password']/parent::div/preceding::div[1]</code></pre>
<p>This moves to the password input's parent div and then searches backward for a preceding div.</p>
<hr>
<h2>45. Parent and Child XPath in Registration Forms</h2>
<h3>HTML Example</h3>
<pre><code><form>
<div class="field">
<label>First Name</label>
<input type="text">
</div>
<div class="field">
<label>Email</label>
<input type="email">
</div>
</form></code></pre>
<p>Locate the Email field container:</p>
<pre><code>//label[text()='Email']/parent::div</code></pre>
<p>Locate the Email input:</p>
<pre><code>//label[text()='Email']/parent::div/child::input</code></pre>
<hr>
<h2>46. Parent and Child XPath in Navigation Menus</h2>
<h3>HTML Example</h3>
<pre><code><nav class="menu">
<a>Home</a>
<a>Products</a>
<a>Services</a>
</nav></code></pre>
<p>Locate the navigation container:</p>
<pre><code>//a[text()='Products']/parent::nav</code></pre>
<p>If the links are direct children of the nav:</p>
<pre><code>//nav[@class='menu']/child::a[text()='Products']</code></pre>
<hr>
<h2>47. Parent and Child XPath in Nested Divs</h2>
<p>Consider:</p>
<pre><code><div class="outer">
<div class="middle">
<div class="inner">
<button>Submit</button>
</div>
</div>
</div></code></pre>
<p>Direct parent of the button:</p>
<pre><code>//button[text()='Submit']/parent::div</code></pre>
<p>Parent of the parent:</p>
<pre><code>//button[text()='Submit']/parent::div/parent::div</code></pre>
<p>Outer ancestor:</p>
<pre><code>//button[text()='Submit']/ancestor::div[@class='outer']</code></pre>
<hr>
<h2>48. Parent and Child XPath in Cards</h2>
<p>Cards are common components in modern web applications.</p>
<pre><code><div class="card">
<h3>Selenium</h3>
<p>Automation Testing</p>
<button>Learn More</button>
</div></code></pre>
<p>Parent card:</p>
<pre><code>//h3[text()='Selenium']/parent::div</code></pre>
<p>Child button:</p>
<pre><code>//div[@class='card']/child::button[text()='Learn More']</code></pre>
<hr>
<h2>49. Parent and Child XPath in Dynamic Applications</h2>
<p>Modern web applications can contain dynamic IDs and class names. In such cases, a stable text element can sometimes be used as an anchor for parent-child navigation.</p>
<pre><code>//label[normalize-space()='Email']/parent::div/child::input</code></pre>
<p>This avoids relying directly on a potentially dynamic input ID.</p>
<p>The locator should still be verified against the actual DOM and application behavior because dynamic applications can change their structure.</p>
<hr>
<h2>50. Parent and Child XPath in Selenium Java</h2>
<p>Selenium Java uses <strong>By.xpath()</strong> to locate elements using XPath.</p>
<pre><code>WebElement parent = driver.findElement(
By.xpath("//input[@id='username']/parent::div")
);</code></pre>
<p>Child example:</p>
<pre><code>WebElement input = driver.findElement(
By.xpath("//div[@class='login-form']/child::input[@id='username']")
);</code></pre>
<hr>
<h2>51. Complete Selenium Example</h2>
<pre><code>import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ParentChildXPathExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebElement parent = driver.findElement(
By.xpath("//input[@id='username']/parent::div")
);
WebElement username = driver.findElement(
By.xpath("//div[@class='login-form']/child::input[@id='username']")
);
username.sendKeys("testuser");
driver.quit();
}
}</code></pre>
<hr>
<h2>52. Parent and Child XPath with Explicit Wait</h2>
<p>Dynamic web applications may require synchronization before interacting with elements.</p>
<pre><code>WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement username = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.xpath("//label[text()='Username']/parent::div/child::input")
)
);
username.sendKeys("testuser");</code></pre>
<p>Explicit waits can help ensure that the required element is available before Selenium interacts with it.</p>
<hr>
<h2>53. Parent XPath for Locating a Container</h2>
<p>Sometimes a child element is easier to identify than its parent container.</p>
<p>Example:</p>
<pre><code>//span[text()='Order #12345']/parent::div</code></pre>
<p>This can locate the parent container associated with the order information.</p>
<hr>
<h2>54. Child XPath for Locating Controls Inside a Container</h2>
<p>After locating a container, child navigation can identify controls inside it.</p>
<pre><code>//div[@class='order-card']/child::button[text()='View Details']</code></pre>
<p>This locates a direct View Details button inside the order card.</p>
<hr>
<h2>55. Parent and Child XPath with Multiple Conditions</h2>
<p>Multiple conditions can be used with parent-child relationships.</p>
<pre><code>//div[@class='form-group' and label[text()='Email']]/child::input[@type='email']</code></pre>
<p>This searches for a form-group div containing an Email label and then selects its email input.</p>
<hr>
<h2>56. Parent-Child XPath with or</h2>
<p>The <strong>or</strong> operator can be used when more than one text value is acceptable.</p>
<pre><code>//div[label[text()='Username'] or label[text()='User Name']]/child::input</code></pre>
<p>This can handle two possible label variations.</p>
<hr>
<h2>57. Parent-Child XPath with Index</h2>
<p>Indexes can be used when multiple child elements match the same condition.</p>
<pre><code>//div[@class='form']/child::input[1]</code></pre>
<p>This selects the first direct input child of the form div.</p>
<p>Another example:</p>
<pre><code>//div[@class='form']/child::input[2]</code></pre>
<p>This selects the second matching direct input child.</p>
<hr>
<h2>58. Parent-Child XPath with Last()</h2>
<p>The <strong>last()</strong> function can select the last matching element.</p>
<pre><code>//div[@class='form']/child::input[last()]</code></pre>
<p>This selects the last matching direct input child of the form div.</p>
<hr>
<h2>59. Parent-Child XPath in Shopping Cart</h2>
<p>Consider a shopping cart item:</p>
<pre><code><div class="cart-item">
<span>Laptop</span>
<span>₹50000</span>
<button>Remove</button>
</div></code></pre>
<p>Locate the cart item using the product name:</p>
<pre><code>//span[text()='Laptop']/parent::div</code></pre>
<p>Locate the Remove button inside the cart item:</p>
<pre><code>//div[@class='cart-item']/child::button[text()='Remove']</code></pre>
<hr>
<h2>60. Parent-Child XPath in Checkout Page</h2>
<p>Checkout forms often contain groups of labels and controls.</p>
<pre><code><div class="checkout-field">
<label>Card Number</label>
<input type="text">
</div></code></pre>
<p>Locate the field container:</p>
<pre><code>//label[text()='Card Number']/parent::div</code></pre>
<p>Locate the input:</p>
<pre><code>//label[text()='Card Number']/parent::div/child::input</code></pre>
<hr>
<h2>61. Parent-Child XPath in Web Tables</h2>
<p>Tables are another common use case for parent-child XPath.</p>
<pre><code><tr>
<td>John</td>
<td>[email protected]</td>
<td>
<button>Edit</button>
</td>
</tr></code></pre>
<p>Locate the row:</p>
<pre><code>//td[text()='John']/parent::tr</code></pre>
<p>Locate the Edit button from that row:</p>
<pre><code>//tr[td[text()='John']]/child::td/child::button[text()='Edit']</code></pre>
<hr>
<h2>62. Parent and Child XPath with Following-Sibling</h2>
<p>Parent-child relationships can be combined with sibling navigation.</p>
<pre><code>//label[text()='Username']/parent::div/following-sibling::div[1]</code></pre>
<p>This finds the next sibling div of the label's parent container.</p>
<hr>
<h2>63. Parent and Child XPath with Preceding-Sibling</h2>
<pre><code>//label[text()='Password']/parent::div/preceding-sibling::div[1]</code></pre>
<p>This finds the previous sibling div of the Password label's parent container.</p>
<hr>
<h2>64. Parent-Child XPath vs Following and Preceding</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Relationship</th><th>XPath Axis</th><th>Direction</th></tr>
<tr><td>Parent</td><td><code>parent::</code></td><td>Up one level</td></tr>
<tr><td>Child</td><td><code>child::</code></td><td>Down one level</td></tr>
<tr><td>Ancestor</td><td><code>ancestor::</code></td><td>Up multiple levels</td></tr>
<tr><td>Descendant</td><td><code>descendant::</code></td><td>Down multiple levels</td></tr>
<tr><td>Following-sibling</td><td><code>following-sibling::</code></td><td>Forward among siblings</td></tr>
<tr><td>Preceding-sibling</td><td><code>preceding-sibling::</code></td><td>Backward among siblings</td></tr>
<tr><td>Following</td><td><code>following::</code></td><td>Forward through document</td></tr>
<tr><td>Preceding</td><td><code>preceding::</code></td><td>Backward through document</td></tr>
</table>
<hr>
<h2>65. Common Mistakes in Parent and Child XPath</h2>
<ul>
<li>Confusing parent with ancestor.</li>
<li>Confusing child with descendant.</li>
<li>Assuming every visually nested element is a direct child.</li>
<li>Ignoring the actual DOM hierarchy.</li>
<li>Using too many parent steps such as <code>../../..</code> without checking the structure.</li>
<li>Using indexes when a more stable locator is available.</li>
<li>Ignoring dynamic changes in the DOM.</li>
<li>Using broad XPath expressions that return multiple elements.</li>
<li>Not testing XPath expressions in browser DevTools.</li>
<li>Depending on unstable attributes unnecessarily.</li>
</ul>
<hr>
<h2>66. Best Practices for Parent and Child XPath</h2>
<ul>
<li>Inspect the DOM before creating the XPath.</li>
<li>Understand the exact parent-child relationship.</li>
<li>Use <code>parent::</code> when moving to the immediate parent.</li>
<li>Use <code>child::</code> when selecting direct children.</li>
<li>Use <code>ancestor::</code> for higher-level containers.</li>
<li>Use <code>descendant::</code> when the target may be at any depth.</li>
<li>Use attributes and text to make the XPath specific.</li>
<li>Prefer stable reference elements.</li>
<li>Avoid unnecessary indexes.</li>
<li>Avoid overly long XPath expressions.</li>
<li>Test the locator in Chrome DevTools.</li>
<li>Use explicit waits for dynamically loaded elements.</li>
<li>Keep XPath expressions readable and maintainable.</li>
</ul>
<hr>
<h2>67. How to Test Parent and Child XPath in Chrome DevTools</h2>
<p>XPath expressions can be tested in the Chrome Developer Tools console using the <strong>$x()</strong> function.</p>
<ol>
<li>Open the web page in Chrome.</li>
<li>Right-click the required element.</li>
<li>Select <strong>Inspect</strong>.</li>
<li>Open the <strong>Console</strong> tab.</li>
<li>Use the <strong>$x()</strong> function.</li>
<li>Enter the XPath expression.</li>
</ol>
<h3>Parent Example</h3>
<pre><code>$x("//input[@id='username']/parent::div")</code></pre>
<h3>Child Example</h3>
<pre><code>$x("//div[@class='login-form']/child::input")</code></pre>
<h3>Parent with Text Example</h3>
<pre><code>$x("//label[text()='Username']/parent::div")</code></pre>
<hr>
<h2>68. Parent and Child XPath Cheat Sheet</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Requirement</th><th>XPath</th></tr>
<tr><td>Direct parent</td><td><code>//input/parent::div</code></td></tr>
<tr><td>Parent using ..</td><td><code>//input/..</code></td></tr>
<tr><td>Direct child</td><td><code>//div/child::input</code></td></tr>
<tr><td>Short child syntax</td><td><code>//div/input</code></td></tr>
<tr><td>Parent with text</td><td><code>//label[text()='Email']/parent::div</code></td></tr>
<tr><td>Child with text</td><td><code>//div/child::button[text()='Login']</code></td></tr>
<tr><td>Parent with attribute</td><td><code>//input[@id='email']/parent::div[@class='field']</code></td></tr>
<tr><td>Child with attribute</td><td><code>//div[@class='field']/child::input[@id='email']</code></td></tr>
<tr><td>Parent and child together</td><td><code>//label[text()='Email']/parent::div/child::input</code></td></tr>
<tr><td>Parent and descendant</td><td><code>//label[text()='Email']/parent::div/descendant::input</code></td></tr>
<tr><td>Parent with contains()</td><td><code>//label[contains(text(),'Email')]/parent::div</code></td></tr>
<tr><td>Child with contains()</td><td><code>//div/child::button[contains(text(),'Login')]</code></td></tr>
<tr><td>First child</td><td><code>//div/child::input[1]</code></td></tr>
<tr><td>Last child</td><td><code>//div/child::input[last()]</code></td></tr>
</table>
<hr>
<h2>69. Interview Questions on Parent and Child XPath</h2>
<ol>
<li>What is parent XPath?</li>
<li>What is child XPath?</li>
<li>What is the parent axis?</li>
<li>What is the child axis?</li>
<li>What is the difference between parent and ancestor?</li>
<li>What is the difference between child and descendant?</li>
<li>How do you move from a child element to its parent?</li>
<li>How do you move from a parent element to its child?</li>
<li>What does <code>..</code> mean in XPath?</li>
<li>What is the difference between <code>child::</code> and a normal slash?</li>
<li>How do you locate a parent div using label text?</li>
<li>How do you locate an input inside a specific parent?</li>
<li>How can parent-child XPath be combined with contains()?</li>
<li>How can parent-child XPath be combined with normalize-space()?</li>
<li>How can parent-child XPath be used with following-sibling?</li>
<li>How can parent-child XPath be used in tables?</li>
<li>How do you test parent-child XPath in Chrome DevTools?</li>
<li>How do you use parent-child XPath in Selenium Java?</li>
<li>What are common mistakes when using parent-child XPath?</li>
<li>What are the best practices for creating parent-child XPath?</li>
</ol>
<hr>
<h2>70. Interview Answers</h2>
<h3>Q1. What is parent XPath?</h3>
<p>Parent XPath is used to navigate from a child element to its immediate parent element using the <code>parent::</code> axis or the <code>..</code> shorthand.</p>
<h3>Q2. What is child XPath?</h3>
<p>Child XPath is used to navigate from a parent element to its direct child elements using the <code>child::</code> axis or normal child path syntax.</p>
<h3>Q3. What is the difference between parent and ancestor?</h3>
<p>Parent identifies only the immediate parent, while ancestor can identify the parent, grandparent, or other higher-level elements.</p>
<h3>Q4. What is the difference between child and descendant?</h3>
<p>Child selects direct children, while descendant can select elements located at any level below the current element.</p>
<h3>Q5. What does .. mean in XPath?</h3>
<p>The <code>..</code> expression represents the parent node of the current context node.</p>
<h3>Q6. Give an example of parent XPath.</h3>
<pre><code>//input[@id='username']/parent::div</code></pre>
<h3>Q7. Give an example of child XPath.</h3>
<pre><code>//div[@class='login-form']/child::input</code></pre>
<hr>
<h2>71. Practical Parent and Child XPath Checklist</h2>
<ul>
<li>Inspect the HTML DOM.</li>
<li>Identify the reference element.</li>
<li>Determine whether you need to move upward or downward.</li>
<li>Use <code>parent::</code> for the immediate parent.</li>
<li>Use <code>child::</code> for direct children.</li>
<li>Use <code>ancestor::</code> for higher-level elements.</li>
<li>Use <code>descendant::</code> for elements at any lower level.</li>
<li>Add text or attributes to make the locator specific.</li>
<li>Use normalize-space() when whitespace is an issue.</li>
<li>Use contains() when partial text is stable.</li>
<li>Check whether multiple elements match.</li>
<li>Avoid unnecessary indexes.</li>
<li>Test XPath using Chrome DevTools.</li>
<li>Use explicit waits for dynamic elements.</li>
<li>Use the final XPath with Selenium's <code>By.xpath()</code>.</li>
</ul>
<hr>
<h2>72. Conclusion</h2>
<p><strong>Parent and Child XPath</strong> provides a structured way to navigate the HTML DOM in Selenium. The <strong>parent::</strong> axis moves from an element to its immediate parent, while the <strong>child::</strong> axis selects direct child elements.</p>
<p>Parent-child XPath becomes especially useful when elements have dynamic attributes or when a stable relationship between elements provides a better locator. By combining parent and child axes with text(), contains(), normalize-space(), attributes, indexes, ancestor, descendant, following, and sibling axes, testers can create flexible XPath expressions for real-world web applications.</p>
<p>For Selenium WebDriver, XPath, CSS selectors, dynamic elements, TestNG, automation frameworks, and practical automation testing learning, visit <a href="https://www.justacademy.co/course-detail/selenium-training" target="_blank" rel="noopener noreferrer">JustAcademy Selenium Training</a>.</p>
<p>To explore the course demonstration registration option, visit <a href="https://www.justacademy.co/register-for-course-demo" target="_blank" rel="noopener noreferrer">JustAcademy Course Demo Registration</a>.</p>
</div>